//
//  RegasterController.m
//  youthNote
//
//  Created by huangrensheng on 16/6/13.
//  Copyright © 2016年 laber. All rights reserved.
//

#import "RegasterController.h"
#import "RegisterCell.h"

@interface RegasterController()

@property (nonatomic, strong) UITableView *registerTable;
@property (nonatomic, strong) NSMutableDictionary *parame;
@end

@implementation RegasterController

-(void)viewDidLoad{
    [super viewDidLoad];
    [self createWithLeftStyle:@{@"type":@"back"} middleStyle:@{@"type":@"title",@"title":@"注册"} rightStyle:nil];
    self.parame = [[NSMutableDictionary alloc] init];
    self.registerTable = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.registerTable.backgroundColor = [UIColor whiteColor];
    self.registerTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.registerTable.dataSource = self;
    self.registerTable.delegate = self;
    self.registerTable.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:self.registerTable];
    [self.registerTable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 64;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] init];
    UIButton *regist = [[UIButton alloc] initWithIcon:nil title:@"注册" textClolor:[UIColor whiteColor] textFont:18];
    [regist addTarget:self action:@selector(regiser) forControlEvents:UIControlEventTouchUpInside];
    [regist setLayerCornerRadius:4 borderColor:[UIColor clearColor] borderWidth:1];
    [regist setBackgroundImage:[UIImage imageWithColor:BAR_COLOR] forState:UIControlStateNormal];
    [regist setBackgroundImage:[UIImage imageWithColor:BAR_SELECT_COLOR] forState:UIControlStateHighlighted];
    [view addSubview:regist];
    [regist mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(view).insets(UIEdgeInsetsMake(20, 15, 0, 15));
        make.height.equalTo(inset(44));
    }];
    return view;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *indetifer = @"RegisterCell";
    RegisterCell *cell = [tableView dequeueReusableCellWithIdentifier:indetifer];
    if (!cell) {
        cell = [[RegisterCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indetifer];
    }
    cell.indexPath = indexPath;
    [cell shouldEditFinish:^(NSString *key, NSString *value) {
        [self.parame setValue:value forKey:key];
    }];
    return cell;
}

- (void)regiser{
    if (!self.parame[@"userName"]) {
        [self.view makeToast:@"请输入用户名"];
        return;
    }else if (!self.parame[@"passWord"]) {
        [self.view makeToast:@"请输入密码"];
        return;
    }else if (!self.parame[@"surePassWord"]) {
        [self.view makeToast:@"请确认密码"];
        return;
    }else if (!self.parame[@"email"]) {
        [self.view makeToast:@"请输入邮箱"];
        return;
    }
    if (![self.parame[@"passWord"] isEqualToString:self.parame[@"surePassWord"]]) {
        [self.view makeToast:@"确认密码不一致"];
        return;
    }
    [[HttpManger shareHttpManage] postWithUrl:@"register" params:self.parame success:^(NSDictionary *data) {
        [self.navigationController popViewControllerAnimated:YES];
        NSLog(@"%@",data);
    } error:^(NSError *error) {
        
    }];
}

@end
